package com.css.zfzx.sjcj.modules.boundary.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.boundary.repository.BoundaryNativeRepository;
import com.css.zfzx.sjcj.modules.boundary.repository.entity.BoundaryEntity;
import com.css.zfzx.sjcj.modules.boundary.service.BoundaryService;
import com.css.zfzx.sjcj.modules.boundary.viewobjects.BoundaryQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author andy
 * @date 2021-01-26
 */
@Service
@Slf4j
public class BoundaryServiceImpl implements BoundaryService {

    @Autowired
    private BoundaryNativeRepository boundaryNativeRepository;

    @Override
    public JSONObject getBoundaries(BoundaryQueryParams queryParams) {
        JSONObject jsonObject = new JSONObject();
        List<BoundaryEntity> boundaryEntities = this.boundaryNativeRepository.listBoundaries(queryParams);
        for (BoundaryEntity boundaryEntity : boundaryEntities) {
            String coords = boundaryEntity.getCoordinate();
            if (coords == null) continue;
            coords = coords.substring(coords.indexOf("(((") + 3, coords.lastIndexOf(")))"));
            coords = coords.replace(")), ((", "|");
            coords = coords.replace("), (", "#");
            coords = coords.replace(", ", ";");
            coords = coords.replace(" ", ",");
            jsonObject.put(boundaryEntity.getName(), coords);
        }
        return jsonObject;
    }
}
